// BaseCoalMineUnit.cpp: implementation of the BaseCoalMineUnit class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BaseCoalMineUnit.h"
#include "BaseMonitoringUnit.h"

#include "CommonLib\Markup.h"
#include "CommonLib\Structure.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

BaseCoalMineUnit::BaseCoalMineUnit(CoalMineUnitManager * pMgr):
	MonitoringUnitManager(),
	m_pMgr(pMgr),
	m_nId(0),
	m_strName(""),
	m_strIPAddr(""),
	m_bConnected(false)
{
}

BaseCoalMineUnit::~BaseCoalMineUnit()
{
}

void BaseCoalMineUnit::ConvertFromXML(Markup& xml)
{
	xml.FindChildElem(_T("Id"));
	m_nId = atoi(xml.GetChildData());

	xml.FindChildElem(_T("Name"));
	m_strName = xml.GetChildData();

	xml.FindChildElem(_T("IPAddr"));
	m_strIPAddr = xml.GetChildData();
}

void BaseCoalMineUnit::ConvertToXML(Markup& xml) const
{
	CString str;

	str.Format("%d", m_nId);
	xml.AddChildElem(_T("Id"), _T(str));

	xml.AddChildElem(_T("Name"), _T(m_strName));

	xml.AddChildElem(_T("IPAddr"), _T(m_strIPAddr));
}

HTREEITEM BaseCoalMineUnit::InsertToTreeCtrl(CTreeCtrl& treectrl, HTREEITEM hItemParent) const
{
	TV_INSERTSTRUCT curTreeItem;

	curTreeItem.hParent = hItemParent;
	curTreeItem.hInsertAfter = TVI_SORT;
	curTreeItem.item.iImage = m_bConnected?2:3;
	curTreeItem.item.iSelectedImage = curTreeItem.item.iImage;
	CString strDesc;
	GetDescription(strDesc);
	curTreeItem.item.pszText = strDesc.LockBuffer();
	curTreeItem.item.lParam = (LPARAM)this;
	curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT | TVIF_PARAM;
	HTREEITEM hItem = treectrl.InsertItem(&curTreeItem);
	strDesc.UnlockBuffer();

	return hItem;
}

int BaseCoalMineUnit::InsertToListCtrl(CListCtrl& listctrl) const
{
	LVITEM curListItem;

	curListItem.mask = LVIF_TEXT | LVIF_PARAM;
	curListItem.iItem = 0;
	curListItem.iSubItem=0;
	CString str;
	str.Format("%03d", m_nId);
	curListItem.pszText = str.LockBuffer();
	curListItem.cchTextMax = lstrlen(curListItem.pszText);
	curListItem.lParam = (LPARAM)this;
	int iActualItem = listctrl.InsertItem(&curListItem);
	str.UnlockBuffer();

	curListItem.mask = LVIF_TEXT;
	curListItem.iItem = iActualItem;
	curListItem.iSubItem = 1;
	str = m_strName;
	curListItem.pszText = str.LockBuffer();
	curListItem.cchTextMax = lstrlen(curListItem.pszText);
	listctrl.SetItem(&curListItem);
	str.UnlockBuffer();

	curListItem.iSubItem = 2;
	str = m_strIPAddr;
	curListItem.pszText = str.LockBuffer();
	curListItem.cchTextMax = lstrlen(curListItem.pszText);
	listctrl.SetItem(&curListItem);
	str.UnlockBuffer();

	listctrl.SetItemState(iActualItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

	return iActualItem;
}

int BaseCoalMineUnit::InsertToComboBox(CComboBox& combobox) const
{
	CString str = GetName();
	int nIndex = combobox.AddString(str);
	combobox.SetItemData(nIndex, GetId());

	return nIndex;
}

void BaseCoalMineUnit::ConvertToStruct(CoalMineInfoItem * pCoalMineInfoItem) const
{
	pCoalMineInfoItem->m_nId = m_nId;
	//ASSERT(m_strName.GetLength() < sizeof(pCoalMineInfoItem->m_szName));
	strcpy(pCoalMineInfoItem->m_szName, m_strName);
	//ASSERT(m_strIPAddr.GetLength() < sizeof(pCoalMineInfoItem->m_szIPAddr));
	strcpy(pCoalMineInfoItem->m_szIPAddr, m_strIPAddr);
	pCoalMineInfoItem->m_bConnected = m_bConnected;
}
void BaseCoalMineUnit::ConvertToStruct(CoalMineInfoItemEx * pCoalMineInfoItem) const
{
	pCoalMineInfoItem->m_nId = m_nId;
	//ASSERT(m_strName.GetLength() < sizeof(pCoalMineInfoItem->m_szName));
	strcpy(pCoalMineInfoItem->m_szName, m_strName);
	//ASSERT(m_strIPAddr.GetLength() < sizeof(pCoalMineInfoItem->m_szIPAddr));
	strcpy(pCoalMineInfoItem->m_szIPAddr, m_strIPAddr);
	pCoalMineInfoItem->m_bConnected = m_bConnected;
	pCoalMineInfoItem->m_stTime = m_stTime;
}
void BaseCoalMineUnit::SetTime()
{
	CTime time = CTime::GetCurrentTime();

	m_stTime.wYear  =  time.GetYear();
	m_stTime.wMonth = time.GetMonth();
	m_stTime.wDay   =  time.GetDay();
	m_stTime.wHour  = time.GetHour();
	m_stTime.wMinute = time.GetMinute();
	m_stTime.wSecond = time.GetSecond();
}

void BaseCoalMineUnit::UpdateDefinition(DefinitionUpdateStruct * pDefinitionUpdateStruct)
{
	MonitoringUnitManager * pMonitoringUnitMgr = new MonitoringUnitManager;
	pMonitoringUnitMgr->ConvertDefFromStruct(pDefinitionUpdateStruct->m_MonUnitDef, pDefinitionUpdateStruct->m_dwNumOfDef);
	ApplyNewDefinition(pMonitoringUnitMgr);
	delete pMonitoringUnitMgr;
}

void BaseCoalMineUnit::UpdateDefinition(DefinitionUpdateStructEx * pDefinitionUpdateStruct)
{
	MonitoringUnitManager * pMonitoringUnitMgr = new MonitoringUnitManager;
	pMonitoringUnitMgr->ConvertDefFromStruct(pDefinitionUpdateStruct->m_MonUnitDef, pDefinitionUpdateStruct->m_dwNumOfDef);
	ApplyNewDefinition(pMonitoringUnitMgr);
	delete pMonitoringUnitMgr;
}

void BaseCoalMineUnit::UpdateData(DataUpdateStruct * pDataUpdateStruct)
{
	for (int i = 0;i < pDataUpdateStruct->m_dwNumOfData;i++)
	{
		MonUnitData * pMonUnitData = &pDataUpdateStruct->m_MonUnitData[i];
		BaseMonitoringUnit * pMonitoringUnit = GetMonitoringUnitById(pMonUnitData->m_szId);
		if (pMonitoringUnit)
		{
			SetData(pMonitoringUnit, CTime(pMonUnitData->m_stData), (MonitoringUnitState::States)pMonUnitData->m_nState, pMonUnitData->m_fValue);
			pMonitoringUnit->GetStateChangeTime().GetAsSystemTime(pMonUnitData->m_stStateChange);
		}
	}
}

void BaseCoalMineUnit::UpdateData(DataUpdateStructEx * pDataUpdateStruct)
{
	for (int i = 0;i < pDataUpdateStruct->m_dwNumOfData;i++)
	{
		MonUnitDataEx * pMonUnitData = &pDataUpdateStruct->m_MonUnitData[i];
		BaseMonitoringUnit * pMonitoringUnit = GetMonitoringUnitById(pMonUnitData->m_szId);
		if (pMonitoringUnit)
		{
			SetData(pMonitoringUnit, CTime(pMonUnitData->m_stData), (MonitoringUnitState::States)pMonUnitData->m_nState, pMonUnitData->m_fValue);
			pMonitoringUnit->GetStateChangeTime().GetAsSystemTime(pMonUnitData->m_stStateChange);
		}
	}
}